# Stop the Node server recorded in .server.pid
Param(
    [string]$ServerDir = (Get-Location).Path
)
Set-Location -Path $ServerDir

$pidFile = Join-Path $ServerDir '.server.pid'
if (-Not (Test-Path $pidFile)) {
    Write-Output "PID file not found: $pidFile"
    exit 1
}

$pid = Get-Content $pidFile | Out-String
$pid = $pid.Trim()
if ([string]::IsNullOrEmpty($pid)) {
    Write-Output "PID file is empty"
    exit 1
}

try {
    Stop-Process -Id $pid -Force -ErrorAction Stop
    Write-Output "Stopped process $pid"
    Remove-Item $pidFile -ErrorAction SilentlyContinue
} catch {
    Write-Output "Failed to stop process $pid: $_"
}
